package cn.jn.lhm.netty.part08.chat.handler;

import cn.jn.lhm.netty.part08.chat.config.UserThread;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author lhm 2753914576@qq.com
 * @time 2023/11/1 - 21:08
 **/
@Slf4j
public class ClientQuitHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        log.info("{}断开连接", UserThread.getUsername());
        ctx.channel().close();
        UserThread.clear();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.info("[{}]异常退出,原因:[{}]", UserThread.getUsername(), cause.getMessage());
        ctx.channel().close();
        UserThread.clear();
    }

}
